<template>
  <DetailsFormBox :title="`${dataInfo?.table?.title}`" :type="data.type" :form="data.form" :btns="[
    {
      title: '重置',
      icon: '',
      type: '',
      click: getForm,
    },
    {
      title: '保存',
      icon: '',
      type: 'primary',
      click: (form: any) => {
        saveBtn(form)
      },
    },
  ]" :loading="data.loading" :json="dataInfo">
    <template #default="{ form }">
      <div class="formMain">
        <ElFormItemBox label="数据编号" :prop="`dicValueCode`">
          <ElInputBox :data="form.dicValueCode" disabled placeholder="系统自动生成" />
        </ElFormItemBox>
        <ElFormItemBox label="数据名称" :prop="`displayValue`" isRules>
          <ElInputBox :data="form" dataName="displayValue" />
        </ElFormItemBox>
        <ElFormItemBox label="排序" :prop="`dicValueSort`">
          <ElNumberBox :data="form" dataName="dicValueSort" />
        </ElFormItemBox>
        <ElFormItemBox label="备注" :prop="`dicValueDesc`">
          <ElInputBox :data="form" dataName="dicValueDesc" type="textarea" />
        </ElFormItemBox>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { useListComponent } from '../pageTs'; // 引入逻辑
const { dataInfo } = useListComponent();
import { reactive, onMounted, getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance() as any;
const data = reactive({
  loading: false,
  loadingMsg: '',
  type: '2',
  form: {
    dicValueID: 0, // 字典值主键ID
    dictionaryCode: "", // 字典代码（唯一识别码）
    dicValueCode: "", // 字典值代码（唯一识别码）
    displayValue: "", // 字典显示值（类似字段名称）
    dicValue: "", // 字典实际值（隐藏值）
    dicValueDesc: "", // 字典值说明
    dicValueSort: 0 // 字典值排序
  },
  fileCodes: []
});
import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
// 文件列表
const getProductCraftsFiles = () => {
  proxy.$api.ProductCraft.GetProductCraftsFileList().then((res: any) => {
    data.fileCodes = res.map((el: any) => ({
      label: el.name,
      value: el.code,
    }));
  });
};

import { sendMsg } from '@/utils/crossTagMag.ts';
const saveBtn = (form: any) => {
  if (form.dicValueID) {
    proxy.$api.System.UpdateDictionaryValue({
      ...form,
    })
      .then((res: any) => {
        _sun()
      })
      .catch(() => { });
  } else {
    proxy.$api.System.AddDictionaryValue({
      ...form,
    })
      .then((res: any) => {
        _sun()
      })
      .catch(() => { });
  }

  function _sun() {
    proxy.$message('保存成功')
    sendMsg('save', true);
    window.close();
  }
};
const getForm = () => {
  let _query: any = route.query
  if (_query.dicValueID) {
    proxy.$api.System.QueryDictionaryValueDetail({
      dicValueID: _query.dicValueID,//获取数据传递的查询参数值（主键ID或唯一识别码）
    })
      .then((res: any) => {
        proxy.$message('获取成功')
        data.form = {
          ...res,
        }
        data.loading = false
      })
      .catch(() => {
        data.loading = false
      });
  } else {
    data.form.dictionaryCode = _query.dictionaryCode
  }

};

onMounted(() => {
  getForm();
  getProductCraftsFiles();
});
</script>
